package com.hanwei.uap.acceptors.api.utils;

import com.hanwei.uap.common.enums.LogType;
import com.hanwei.uap.acceptors.api.constants.DeviceConstant;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;

public class LogUtils {
    public static String channelLogInfo(LogType logType, Channel channel, String serverHost, int serverPort) {
        StringBuffer logInfo = new StringBuffer();
        String channelId = channel.id().asShortText();
        InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
        String clientHost = remoteAddress.getAddress().getHostAddress();
        int clientPort = remoteAddress.getPort();
        String deviceId = ChannelUtils.getDeviceId(channel);
        if (null == deviceId)
            deviceId = DeviceConstant.UNSET;
        String pattern = "ChannelId:%s, ClientIpPort:%-15s:%5d, ServerIpPort:%-15s:%5d, DeviceId:%-16s, LogType:%-20s";
        return String.format(pattern, channelId, clientHost, clientPort, serverHost, serverPort, deviceId, logType);
    }
}
